package com.ruoyi.web.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.novel.domain.*;
import com.ruoyi.novel.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 首页相关接口
 */

@RestController
@RequestMapping("/app/index")
public class AppIndexController {

    @Autowired
    private IConfigService configService;

    @Autowired
    private ITypeService typeService;

    @Autowired
    private IVideoService videoService;

    @Autowired
    private IBannerService bannerService;

    @Autowired
    private IFriendsService friendsService;

    @Autowired
    private ICommunityService communityService;

    @Autowired
    private INovelService novelService;

    @Autowired
    private IVipService vipService;

    @Autowired
    private IIntegralService integralService;

    @Autowired
    private IMatterService matterService;

    @Autowired
    private ISearchService searchService;

    @Autowired
    private IInvitationService invitationService;

    /**
     * 公共配置
     * @return
     */
    @GetMapping("/getConfigList")
    public AjaxResult getConfigList(){
        List<Config> list = configService.list(new QueryWrapper<Config>().orderByAsc("id"));
        return AjaxResult.success(list);
    }

    /**
     * 轮播图列表
     * @param state 0视频 1社区 2小说 3漫画
     * @return
     */
    @GetMapping("/getBannerList")
    public AjaxResult getBannerList(Integer state){
        List<Banner> bannerList = bannerService.list(new QueryWrapper<Banner>().eq("state", state));
        return AjaxResult.success(bannerList);
    }

    /**
     * 获取分类列表
     * @param mode 0视频 1社区 2小说 3漫画
     * @param state 0首页 1精选
     * @param typeId 0顶级分类
     * @return
     */
    @GetMapping("/getTypeList")
    public AjaxResult getTypeList(Integer mode, Integer state, Integer typeId){
        QueryWrapper<Type> queryWrapper = new QueryWrapper<>();
        if(typeId == null || typeId == 0){
            queryWrapper.eq("p_id", 0);
        }else {
            queryWrapper.eq("p_id", typeId);
        }
        if(state != null){
            queryWrapper.eq("state", state);
        }
        queryWrapper.eq("mode",mode).orderByAsc("sort");
        List<Type> list = typeService.list(queryWrapper);
        return AjaxResult.success(list);
    }

    /**
     * 获取视频列表
     * @param type1
     * @param type2
     * @param name
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getVideoList")
    public AjaxResult getVideoList(Integer type1, Integer type2, String name, Integer pageNo, Integer pageSize){
        IPage<Video> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "type1", "type2", "user_id", "name", "content", "image");
        if(type1 != null && type1 > 0){
            queryWrapper.eq("type1", type1);
        }
        if(type2 != null && type2 > 0){
            queryWrapper.eq("type2", type2);
        }
        if(StringUtils.isNotEmpty(name)){
            queryWrapper.like("name", name);
            Long userId = SecurityUtils.getUserIdNotLogin();
            if(userId > 0){
                Search search = new Search();
                search.setUserId(Math.toIntExact(userId));
                search.setTitle(name);
                search.setMode(0);
                search.setCreateTime(new Date());
                searchService.save(search);
            }
        }
        queryWrapper.eq("state",0).orderByAsc("sort").orderBy(true, true, "RAND() * id");
        IPage<Video> videoIPage = videoService.page(page, queryWrapper);
        return AjaxResult.success(videoIPage);
    }

    /**
     * 获取交友信息列表
     * @param name
     * @param region
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getFriendsList")
    public AjaxResult getFriendsList(String name,String region,Integer pageNo, Integer pageSize){
        IPage<Friends> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Friends> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "user_id", "nick_name", "image", "hobby", "content", "province", "city", "region", "location");
        if(StringUtils.isNotEmpty(name)){
            queryWrapper.like("nick_name", name);
        }
        if(StringUtils.isNotEmpty(region)){
            queryWrapper.like("region", region);
        }
        queryWrapper.eq("state",0).orderByAsc("create_time").orderBy(true, true, "RAND() * id");
        IPage<Friends> friendsIPage = friendsService.page(page, queryWrapper);
        return AjaxResult.success(friendsIPage);
    }

    /**
     * 获取社区文章列表
     * @param title
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getCommunityList")
    public AjaxResult getCommunityList(String title, Integer type2,Integer pageNo, Integer pageSize ){
        IPage<Community> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Community> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(title)){
            queryWrapper.like("title", title);
            Long userId = SecurityUtils.getUserIdNotLogin();
            if(userId > 0){
                Search search = new Search();
                search.setUserId(Math.toIntExact(userId));
                search.setTitle(title);
                search.setMode(1);
                search.setCreateTime(new Date());
                searchService.save(search);
            }
        }
        if(type2 != null && type2 > 0){
            queryWrapper.eq("type2", type2);
        }
        queryWrapper.eq("state",0).orderByDesc("create_time").orderBy(true, true, "RAND() * id");
        IPage<Community> communityIPage = communityService.getCommunityList(page, queryWrapper, Math.toIntExact(SecurityUtils.getUserIdNotLogin()));
        return AjaxResult.success(communityIPage);
    }

    /**
     *
     * 获取小说信息
     * @param title
     * @param mode 0小说 1漫画
     * @param state 0正常 1推荐
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getNovelList")
    public AjaxResult getNovelList(String title, Integer mode, Integer state ,Integer pageNo, Integer pageSize){
        IPage<Novel> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Novel> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(title)){
            queryWrapper.like("n.title", title);
            Long userId = SecurityUtils.getUserIdNotLogin();
            if(userId > 0){
                Search search = new Search();
                search.setUserId(Math.toIntExact(userId));
                search.setTitle(title);
                search.setMode(mode+2);
                search.setCreateTime(new Date());
                searchService.save(search);
            }
        }
        queryWrapper.eq("n.mode",mode).eq("n.state",state).lt("n.state", 10).eq("n.p_id",0).orderByDesc("n.create_time").orderBy(true, true, "RAND() * n.id");
        IPage<Novel> novelIPage = novelService.getNovelList(page, queryWrapper);
        return AjaxResult.success(novelIPage);
    }

    /**
     * 获取章节详情
     * @param novelId
     * @return
     */
    @GetMapping("/getNovelInfo")
    public AjaxResult getNovelInfo(Integer novelId){
        Novel novel = novelService.getById(novelId);
        return AjaxResult.success(novel);
    }

    /**
     * 获取VIP商品列表
     * @return
     */
    @GetMapping("/getVipList")
    public AjaxResult getVipList(){
        List<Vip> vipList = vipService.list(new QueryWrapper<Vip>().eq("state", 0));
        return AjaxResult.success(vipList);
    }

    /**
     * 获取积分商品列表
     * @return
     */
    @GetMapping("/getIntegralList")
    public AjaxResult getIntegralList(){
        List<Integral> integralList = integralService.list(new QueryWrapper<Integral>().eq("state", 0));
        return AjaxResult.success(integralList);
    }

    /**
     * 获取常见问题列表
     * @return
     */
    @GetMapping("/getMatterList")
    public AjaxResult getMatterList(){
        List<Matter> matterList = matterService.list(new QueryWrapper<Matter>().eq("state", 0));
        return AjaxResult.success(matterList);
    }

    /**
     * 根据Code获取邀请信息
     * @param code
     * @return
     */
    @GetMapping("/getInvitationByCode")
    public AjaxResult getInvitationByCode(String code){
        List<Invitation> list = invitationService.list(new QueryWrapper<Invitation>().eq("code", code).eq("state",0));
        if(!list.isEmpty()){
            return AjaxResult.success(list.get(0));
        }else {
            return AjaxResult.error("链接已失效");
        }
    }
}
